#!/bin/bash

# 打印系统当前所有进程：进程号、名字
function print_all_processes() {
    echo "当前所有进程："
    echo "进程号   名字"
    echo "-----------------"
    
    for pid in /proc/[0-9]*; do
        if [ -d "$pid" ]; then
            process_name=$(cat "$pid/cmdline" 2>/dev/null)
            process_name=${process_name##*/}
            process_name=${process_name%% *}
            echo "$pid   $process_name"
        fi
    done
}

# 根据程序参数，杀死指定进程
function kill_process_by_pid() {
    local pid_to_kill=$1
    if [ -d "/proc/$pid_to_kill" ]; then
        echo "杀死进程 $pid_to_kill"
        kill -9 "$pid_to_kill"
    else
        echo "进程 $pid_to_kill 不存在"
    fi
}

# 主程序
if [ $# -eq 0 ]; then
    print_all_processes
elif [ $# -eq 1 ]; then
    pid_to_kill=$1
    kill_process_by_pid "$pid_to_kill"
else
    echo "用法： $0 [进程号]"
fi

